package zhongbo.javaweb.instagram.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;

/**
 * 说明： 主定时器
 *
 * @author zhongbo
 * @since 2019年10月04日 星期五 16:49:45
 */
@Component
@Slf4j
public class MainSchedule {

    @Scheduled(fixedRate = 60000)
    public void showMemory() {
        OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
        MemoryMXBean memoryMxBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMxBean.getHeapMemoryUsage();
        MemoryUsage nonHeapMemoryUsage = memoryMxBean.getNonHeapMemoryUsage();

        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        long maxMemory = runtime.maxMemory();
        long totalMb = totalMemory / (1024 * 1024);
        long freeMb = freeMemory / (1024 * 1024);
        long maxMb = maxMemory / (1024 * 1024);
        String pid = System.getProperties().getProperty("PID");

        log.info("内存空间 total: {}MB free: {}MB max: {}MB pid: {}", totalMb, freeMb, maxMb, pid);

    }
}
